﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;

namespace GDCommon.Mail
{
    public class SmptMailHelper
    {
        String host ="";//Mail "192.168.21.134";

        /// <summary>
        /// 主机，例如smtp.126.com
        /// </summary>
        public String Host
        {
            get { return host; }
            set { host = value; }
        }

        int port = 25;
        /// <summary>
        /// 端口：可以不设置
        /// </summary>
        public int Port
        {
            get { return port; }
            set { port = value; }
        }
        String sender = "lux-hollywood@lux-hollywood.com";

        /// <summary>
        /// 发送人的邮箱
        /// </summary>
        public String Sender
        {
            get { return sender; }
            set { sender = value; }
        }
        String senderName = "Lux-hollywood";
        /// <summary>
        /// 需要显示发送人的名字
        /// </summary>
        public String SenderName
        {
            get { return senderName; }
            set { senderName = value; }
        }
        String senderPwd = "lux88123";
        /// <summary>
        /// 发送者的密码
        /// </summary>
        public String SenderPwd
        {
            get { return senderPwd; }
            set { senderPwd = value; }
        }
        String reciever = "xianming.wan@adchina.com";
        /// <summary>
        /// 接收人的邮件地址，多个接收人的话就用;分隔
        /// </summary>
        public String Reciever
        {
            get { return reciever; }
            set { reciever = value; }
        }
        String subject = "";
        /// <summary>
        /// 邮件标题
        /// </summary>
        public String Subject
        {
            get { return subject; }
            set { subject = value; }
        }
        String body = "<b>Test</b>中文会乱码吗？";
        /// <summary>
        /// 邮件内容
        /// </summary>
        public String Body
        {
            get { return body; }
            set { body = value; }
        }

        /// <summary>
        /// 发送
        /// </summary>
        public void Send()
        {
            SmtpClient client = new SmtpClient(host, port);
            MailAddress addrFrom = new MailAddress(sender, senderName, System.Text.Encoding.Default);



            MailMessage message = new MailMessage();


            message.From = addrFrom;
            message.Sender = addrFrom;

            String[] arrReciever = reciever.Split(';');
            foreach (String r in arrReciever)
            {
                if (!String.IsNullOrEmpty(r))
                {
                    message.To.Add(new MailAddress(r));
                }
            }

            message.Body = body;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.Subject = subject;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
            client.UseDefaultCredentials = true;
            client.Credentials = new System.Net.NetworkCredential(sender, senderPwd);
            client.Send(message);
        }
    }
}

